﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using crm_library.DataAccess.TableReps;
using crm_library.DataAccess.ObjectReps;
using crm_library.DataAccess;
using System.Web.UI.WebControls;
using System.Runtime.Serialization;

namespace crm_dal.Objects
{
	[DataContractAttribute]
	public class User : ObjectRepresentation
	{
		#region constructor
		public new const string TableName = "User";
		public new const string ObjectName = "User";

		public User(int userID) : base(TableName, ObjectName)
		{
			UserID = userID;
			Load();
		}

		public User()
			: base(TableName, ObjectName)
		{

		}
		#endregion constructor

		#region properties
		[ListingFieldAttribute("User ID")]
		[DataFieldAttribute("UserID", false, true)]
		[DataMember]
		public int UserID { get; set; }

		[DataFieldAttribute("FirstName", false)]
		[DataMember]
		public string FirstName { get; set; }

		[DataFieldAttribute("LastName", false)]
		[DataMember]
		public string LastName { get; set; }

		[ListingFieldAttribute("Login")]
		[DataFieldAttribute("LoginName", false)]
		[DataMember]
		public string LoginName { get; set; }

		[DataFieldAttribute("ASPNetSecurity", false)]
		[DataMember]
		public Guid ASPNetSecurity { get; set; }

		[DataFieldAttribute("IsAdmin", false)]
		[DataMember]
		public bool IsAdmin { get; set; }

		[DataFieldAttribute("IsMgr", false, true)]
		[DataMember]
		public bool IsMgr { get; set; }

		[DataFieldAttribute("IsCSR", false, true)]
		[DataMember]
		public bool IsCSR { get; set; }

		[DataFieldAttribute("IsSA", false, true)]
		[DataMember]
		public bool IsSA { get; set; }

		#endregion properties

		#region ObjectRepresentation implementations
		public override int GetPKValue()
		{
			return UserID;
		}

		public override void SetPKValue(int pk)
		{
			UserID = pk;
		}

		public override int GetNextPKValue()
		{
			int maxInt = 0;

			foreach (var item in DB.ReadObjectList(new User()))
			{
				maxInt = Math.Max(maxInt, item.GetPKValue());
			}
			return maxInt + 1;
		}

		public override void Load(int pk)
		{
			DB.ReadObject(this, pk);
		}

		public override void Update()
		{
			DB.UpdateObject(this, GetPKValue());
		}

		public override void Delete(int pk)
		{
			DB.DeleteObject(this, pk);
		}

		public override void Create()
		{
			DB.CreateObject(this);
		}

		public override ListItem ToListItem()
		{
			return new ListItem(LoginName, GetPKValue().ToString());
		}


		#endregion ObjectRepresentation implementations

		public static List<User> GetAll()
		{
			return DB.ReadObjectList(new User());
		}
	}
}
